<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/database.php';
require_once '../includes/auth.php';
require_once '../includes/attendance.php';

// 检查权限
$auth = getAuth();
$auth->requireLogin();

// 获取当前用户信息
$currentUser = $auth->getCurrentUser();

// 获取考勤实例
$attendance = getAttendance();

// 设置默认查询参数
$startDate = $_GET['start_date'] ?? date('Y-m-01');
$endDate = $_GET['end_date'] ?? date('Y-m-d');

// 获取考勤记录
$records = $attendance->getUserRecords($currentUser['id'], $startDate, $endDate, 100);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的考勤 - 考勤管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .sidebar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            min-height: 100vh;
            position: fixed;
            width: 250px;
        }
        .sidebar .nav-link {
            color: white;
            padding: 0.75rem 1rem;
            border-radius: 0.375rem;
            margin: 0.125rem 0.5rem;
        }
        .sidebar .nav-link:hover, .sidebar .nav-link.active {
            background: rgba(255,255,255,0.1);
        }
        .sidebar .nav-link i {
            width: 20px;
            text-align: center;
            margin-right: 0.5rem;
        }
        .main-content {
            margin-left: 250px;
            padding: 20px;
        }
        .navbar-custom {
            background: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <div class="sidebar">
        <div class="p-3 text-center border-bottom">
            <h4 class="mb-0">
                <i class="fas fa-clock me-2"></i>
                考勤系统
            </h4>
            <small>用户面板</small>
        </div>
        
        <nav class="nav flex-column p-2">
            <a href="dashboard.php" class="nav-link">
                <i class="fas fa-tachometer-alt"></i>
                仪表板
            </a>
            <a href="attendance.php" class="nav-link active">
                <i class="fas fa-clipboard-list"></i>
                我的考勤
            </a>
            <a href="profile.php" class="nav-link">
                <i class="fas fa-user"></i>
                个人信息
            </a>
            <div class="mt-auto p-2">
                <a href="../public/logout.php" class="nav-link text-warning">
                    <i class="fas fa-sign-out-alt"></i>
                    退出登录
                </a>
            </div>
        </nav>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
        <!-- 顶部导航 -->
        <nav class="navbar navbar-expand navbar-custom mb-4">
            <div class="container-fluid">
                <div class="navbar-nav me-auto">
                    <span class="navbar-text">
                        <i class="fas fa-clipboard-list me-2"></i>我的考勤记录
                    </span>
                </div>
                <div class="navbar-nav">
                    <span class="navbar-text me-3">
                        <i class="fas fa-user me-2"></i>
                        <?php echo htmlspecialchars($currentUser['full_name']); ?>
                    </span>
                    <a href="profile.php" class="nav-link">
                        <i class="fas fa-cog"></i> 个人设置
                    </a>
                </div>
            </div>
        </nav>

        <!-- 查询表单 -->
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">
                    <i class="fas fa-search me-2"></i>查询条件
                </h5>
            </div>
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-md-4">
                        <label for="start_date" class="form-label">开始日期</label>
                        <input type="date" class="form-control" id="start_date" name="start_date" 
                               value="<?php echo htmlspecialchars($startDate); ?>">
                    </div>
                    <div class="col-md-4">
                        <label for="end_date" class="form-label">结束日期</label>
                        <input type="date" class="form-control" id="end_date" name="end_date" 
                               value="<?php echo htmlspecialchars($endDate); ?>">
                    </div>
                    <div class="col-md-4 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary me-2">
                            <i class="fas fa-search me-2"></i>查询
                        </button>
                        <a href="attendance.php" class="btn btn-secondary">
                            <i class="fas fa-refresh me-2"></i>重置
                        </a>
                    </div>
                </form>
            </div>
        </div>

        <!-- 考勤记录表格 -->
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="card-title mb-0">
                    <i class="fas fa-list me-2"></i>考勤记录
                </h5>
                <div>
                    <a href="attendance.php?export=1&start_date=<?php echo $startDate; ?>&end_date=<?php echo $endDate; ?>" 
                       class="btn btn-success btn-sm">
                        <i class="fas fa-download me-2"></i>导出Excel
                    </a>
                </div>
            </div>
            <div class="card-body">
                <?php if (empty($records)): ?>
                    <div class="text-center text-muted py-4">
                        <i class="fas fa-inbox fa-3x mb-3"></i>
                        <p>暂无考勤记录</p>
                    </div>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>日期</th>
                                    <th>星期</th>
                                    <th>上班时间</th>
                                    <th>下班时间</th>
                                    <th>状态</th>
                                    <th>工作时长</th>
                                    <th>备注</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($records as $record): ?>
                                <tr>
                                    <td><?php echo $record['record_date']; ?></td>
                                    <td>
                                        <?php
                                        $weekDays = ['日', '一', '二', '三', '四', '五', '六'];
                                        $weekDay = $weekDays[date('w', strtotime($record['record_date']))];
                                        echo '星期' . $weekDay;
                                        ?>
                                    </td>
                                    <td><?php echo $record['clock_in'] ?: '--'; ?></td>
                                    <td><?php echo $record['clock_out'] ?: '--'; ?></td>
                                    <td>
                                        <?php
                                        $statusLabels = [
                                            'normal' => '<span class="badge bg-success">正常</span>',
                                            'late' => '<span class="badge bg-warning">迟到</span>',
                                            'early_leave' => '<span class="badge bg-warning">早退</span>',
                                            'absent' => '<span class="badge bg-danger">缺勤</span>',
                                            'leave' => '<span class="badge bg-info">请假</span>',
                                            'business_trip' => '<span class="badge bg-primary">出差</span>'
                                        ];
                                        echo $statusLabels[$record['status']] ?? '<span class="badge bg-secondary">未知</span>';
                                        ?>
                                    </td>
                                    <td><?php echo $record['work_hours'] ? $record['work_hours'] . '小时' : '--'; ?></td>
                                    <td><?php echo htmlspecialchars($record['notes'] ?: ''); ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-3 text-muted">
                        <small>共 <?php echo count($records); ?> 条记录</small>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
